package cn.mumzone.pos.ui.fragment.member;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.base.BaseFragment;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.R;
import cn.mumzone.pos.event.DepositGoodsNumEvent;
import cn.mumzone.pos.model.DepositGoodsEntity;
import cn.mumzone.pos.presenter.DepositGoodsPresenter;
import cn.mumzone.pos.presenter.contract.DepositContract;
import cn.mumzone.pos.ui.adapter.ADA_DepositGoods;
import cn.mumzone.pos.ui.widgets.others.StoreHouseHeader;
import cn.mumzone.pos.ui.widgets.refresh.SmartRefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.api.RefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.listener.OnRefreshListener;

public class FRA_DepositGoods extends BaseFragment<DepositGoodsPresenter> implements DepositContract.View {
    @BindView(R.id.rlv_deposit_receivable)
    RecyclerView rlvDepositReceivable;
    @BindView(R.id.rlv_deposit_finished)
    RecyclerView rlvDepositFinished;
    @BindView(R.id.no_deposit_layout)
    LinearLayout noDepositLayout;
    private String memberId;

    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;

    @BindView(R.id.ns_have_times_card)
    NestedScrollView nsHaveTimesCard;

    @Override
    protected void onUserVisible() {
    }

    @Override
    protected void onFirstUserVisible() {
        super.onFirstUserVisible();
        smartRefreshLayout.autoRefresh();
    }


    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        memberId = extras.getString("memberId");
    }

    @Override
    protected void initEventAndData() {
        rlvDepositReceivable.setNestedScrollingEnabled(false);
        rlvDepositFinished.setNestedScrollingEnabled(false);
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.consignedGoods(memberId);
            }
        });
        mPresenter.initEvent();
    }


    @Override
    protected int getLayout() {
        return R.layout.fra_deposit;
    }

    @Override
    public void setDepositGoods(List<DepositGoodsEntity> consignedGoods) {

        if (consignedGoods == null || consignedGoods.isEmpty()) {
            nsHaveTimesCard.setVisibility(View.GONE);
            noDepositLayout.setVisibility(View.VISIBLE);
        } else {
            nsHaveTimesCard.setVisibility(View.VISIBLE);
            noDepositLayout.setVisibility(View.GONE);

            rlvDepositReceivable.setLayoutManager(new LinearLayoutManager(getActivity()));
            rlvDepositFinished.setLayoutManager(new LinearLayoutManager(getActivity()));

            List<DepositGoodsEntity> receiveConsignedGoods = new ArrayList<>();
            List<DepositGoodsEntity> receiveFinishedConsignedGoods = new ArrayList<>();
            for (int i = 0; i < consignedGoods.size(); i++) {
                if (consignedGoods.get(i).getConsignNum() - consignedGoods.get(i).getSendNum() != 0) {//没领完
                    receiveConsignedGoods.add(consignedGoods.get(i));
                } else {//已领完
                    receiveFinishedConsignedGoods.add(consignedGoods.get(i));
                }
            }

            RxBus.getDefault().post(new DepositGoodsNumEvent(String.valueOf(consignedGoods.size() - receiveFinishedConsignedGoods.size())));

            ADA_DepositGoods depositGoodsReceiveAdapter = new ADA_DepositGoods(getActivity());
            rlvDepositReceivable.setAdapter(depositGoodsReceiveAdapter);
            depositGoodsReceiveAdapter.setDataList(receiveConsignedGoods);

            ADA_DepositGoods depositGoodsReceiveFinishedAdapter = new ADA_DepositGoods(getActivity());
            rlvDepositFinished.setAdapter(depositGoodsReceiveFinishedAdapter);
            depositGoodsReceiveFinishedAdapter.setDataList(receiveFinishedConsignedGoods);
        }
    }

    @Override
    public void dismissLoading() {
        smartRefreshLayout.finishRefresh();
    }

    @Override
    protected View getTargetView() {
        return smartRefreshLayout;
    }

    @Override
    protected void refreshNetwork() {
        smartRefreshLayout.autoRefresh();
    }

    @Override
    public void showNetError() {
        smartRefreshLayout.finishRefresh();

        CommonAdapter receivableAdapter = (CommonAdapter) rlvDepositReceivable.getAdapter();
        CommonAdapter finishedAdapter = (CommonAdapter) rlvDepositFinished.getAdapter();

        if (ListUtil.isListEmpty(receivableAdapter.getDataList())
                && ListUtil.isListEmpty(finishedAdapter.getDataList())) {
            super.showNetError();
        } else {
            PosToast.INSTANCE.makeShortToast(mContext, "网络不给力，请稍后重试");
        }
    }

}
